﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataStruct
{
    /// <summary>
    /// 队列
    /// 队列空条件 front=rear
    /// 队列满条件 rear=size-1
    /// </summary>
    internal class Queue
    {
        int[] queues;
        int size;
        int front;//队头
        int rear;//队尾
        public Queue()
        {
            size = 10;
            queues = new int[size];
            front = -1;
            rear = -1;
        }

        /// <summary>
        /// 入队
        /// </summary>
        /// <param name="data"></param>
        public void EnQueue(int data)
        {
            if (rear == (size - 1))
            {
                int[] newArr = new int[size * 2];
                size = size * 2;
                for (int i = 0; i < rear; i++)
                {
                    newArr[i] = queues[i];
                }
                queues = newArr;
            }
            queues[++rear] = data;
        }

        /// <summary>
        /// 出队
        /// </summary>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public int Dequeue()
        {
            if (front == rear)
            {
                throw new Exception("队列为空！");
            }
            return queues[++front];
        }

        /// <summary>
        /// 队列是否为空
        /// </summary>
        /// <returns></returns>
        public bool IsEmpty()
        {
            return front == rear;
        }

        /// <summary>
        /// 查看队尾元素
        /// </summary>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public int Peek()
        {
            if (front == rear)
            {
                throw new Exception("队列为空！");
            }
            return queues[front + 1];
        }

        public void Print()
        {
            if (front == rear)
            {
                throw new Exception("队列为空！");
            }

            for (int i = front + 1; i <= rear; i++)
            {
                Console.Write(queues[i] + " ");
            }
            Console.WriteLine($"队头:{queues[front + 1]},队尾:{queues[rear]},front:{front},rear:{rear}");
        }
    }
}
